#!/bin/bash

# Slackware build script for jbig2dec

# Originally written by Hubert Hesse <email removed>

# Now maintained by B. Watson <urchlay@slackware.uk>

# Original version of this script had no license. Modified version
# licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/
# for details.

# 20211124 bkw: BUILD=2, remove .la file
# 20201103 bkw: update for v0.19
# 20200314 bkw: update for v0.18
# 20191208 bkw: update for v0.17
# 20180904 bkw: update for v0.15

# 20180623 bkw:
# - upstream moved again (to github). use github url for source.
#   the innards of the tarball are slightly different: none of the
#   autoconf-generated stuff is there. however all the actual source
#   code is identical, so I'm not updating BUILD.

# 20171222 bkw:
# - update for v0.14
# - add WTFPL
# - use upstream's homepage URL (they updated it this release)

# 20160725 bkw:
# - update for v0.13
# - removed libpng patch (no longer needed)

# 20150915 bkw:
# - update for v0.12

# Modified 20130620 by B. Watson:
# - Took over maintenance
# - Got rid of static library
# - Use 'make install-strip'
# - Added 'how to edit' stuff to slack-desc

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=jbig2dec
VERSION=${VERSION:-0.19}
BUILD=${BUILD:-2}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.gz
cd $PRGNAM-$VERSION
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

# 20180623 bkw: configure & friends are now absent, generate them.
./autogen.sh

autoreconf -if

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --mandir=/usr/man \
  --docdir=/usr/doc/$PRGNAM-$VERSION \
  --disable-static \
  --enable-shared \
  --build=$ARCH-slackware-linux

make
make install-strip DESTDIR=$PKG
gzip -9 $PKG/usr/man/man1/*
rm -f $PKG/usr/lib*/*.la

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a README LICENSE COPYING CHANGES $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
